﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Web;
using BaseData;
using BaseData.Exts;
using BaseData.HelperEzui;
using BaseData.Models;
using BaseLib.Helper;
using BaseUtility;

namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// NewsAdminGroupFormSubmit 的摘要说明
    /// </summary>
    public class NewsAdminGroupNodeFormSubmit : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            context.Response.Write(this.Get(context).ObjectsToJson());
            context.Response.ContentType = "text/plain";
        }
        MsgErr Get(HttpContext context)
        {
            var error = new MsgErr() { };
            var flag = 0;
            var nodeId = context.Request.QueryString["id"].ToInt(0);
            var action = (Enums.Action)Enum.Parse(typeof(Enums.Action), context.Request.QueryString["action"], true);
            var Title = context.Request.Form["Title"].ToNull(); if (Title.IsNullOrWhiteSpace()) flag++;
            var Folder = context.Request.Form["Folder"].ToNull();
            var Url = context.Request.Form["Url"].ToNull();
            var FlagStatus = context.Request.Form["FlagStatus"].ToInt();
            var flagMenu = context.Request.Form["flagMenu"].ToInt();
            var TemplateIndex = context.Request.Form["TemplateIndex"].ToNull();
            var TemplateNode = context.Request.Form["TemplateNode"].ToNull();
            var TemplateList = context.Request.Form["TemplateList"].ToNull();
            var TemplateView = context.Request.Form["TemplateView"].ToNull();
            if (flag != 0) return new MsgErr() { message = new StringBuilder("标题不能为空！") };
            using (var ctx = new ContextExt())
            {
                try
                {
                    Web_Base_News_Node nodeInit;
                    switch (action)
                    {
                        case Enums.Action.ActNew:
                            nodeInit = new Web_Base_News_Node()
                            {
                                ParentId = nodeId,
                                Title = Title,
                                Folder = Folder,
                                Url = Url,
                                // DateTimeCreate = DateTime.Now,
                                FlagStatus = FlagStatus,
                                FlagView = flagMenu,
                                TemplateIndex = TemplateIndex,
                                TemplateNode = TemplateNode,
                                TemplateList = TemplateList,
                                TemplateView = TemplateView,
                            };
                            ctx.Web_Base_News_Node.AddOrUpdate(m => m.PkId, nodeInit);
                            break;
                        case Enums.Action.ActMove:
                            // nodeInit.ParentId = nodeId;
                            nodeInit = new Web_Base_News_Node();
                            break;
                        default:
                            var nodes = from q in ctx.Web_Base_News_Node select q;
                            if (nodes.Any() && !nodes.Any(w => w.PkId == nodeId)) return new MsgErr() { message = new StringBuilder("指定的节点ID不存在！") };
                            nodeInit = nodes.FirstOrDefault(w => w.PkId == nodeId);
                            // nodeInit.ParentId = nodeId;
                            nodeInit.Title = Title;
                            nodeInit.Folder = Folder;
                            nodeInit.Url = Url;
                            // nodeInit.DateTimeCreate = DateTime.Now;
                            nodeInit.FlagStatus = FlagStatus;
                            nodeInit.FlagView = flagMenu;
                            nodeInit.TemplateIndex = TemplateIndex;
                            nodeInit.TemplateNode = TemplateNode;
                            nodeInit.TemplateList = TemplateList;
                            nodeInit.TemplateView = TemplateView;
                            break;
                    }
                    ctx.SaveChanges();
                    return new MsgErr() { error = 0, id = nodeInit.PkId, text = Title, attributes = new Attributes { key = nodeInit.PkId, value = nodeInit.PkId, }, message = new StringBuilder("节点操作成功了！") };
                }
                catch (Exception err)
                {
                    return new MsgErr() { message = new StringBuilder("错误：" + err.Message), };
                }
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}